package com.coordinate.servlets;
import com.oreilly.servlet.multipart.FilePart;

import com.coordinate.core.factory.AppStackFactory;
import com.coordinate.core.kernel.serviceprovider.ServiceProvider;
import com.coordinate.core.message.*;
import com.coordinate.core.message.impl.*;


import com.oreilly.servlet.multipart.MultipartParser;
import com.oreilly.servlet.multipart.Part;
import com.google.appengine.api.datastore.Blob;

import com.coordinate.db.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import org.apache.commons.io.IOUtils;
import org.datanucleus.store.query.Query;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
import static com.google.appengine.api.labs.taskqueue.TaskOptions.Builder.*;

import com.coordinate.services.*;
import com.coordinate.servlets.VoiceMessage;

import com.coordinate.core.message.*;
import com.coordinate.core.message.impl.*;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.google.appengine.api.datastore.Text;

@SuppressWarnings("serial")
public class VoiceMessageServiceServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		CVoiceMessage lVm = null;
		
		lVm = new CVoiceMessage();
		
	    MultipartParser lMultiPartParser = new MultipartParser(req, 1000000);
	    
	    while(true)
	    {
	    	Part lPart = lMultiPartParser.readNextPart();
	    	
	    	if(null == lPart)
	    	{
	    		break;
	    	}
	    	else if(lPart.isParam() == true)
	    	{
	    		/*handle the parameter here*/
	    		
	    	}
	    	else
	    	{
	    		/*Should be a file ?*/
	    		FilePart lFilePart = (FilePart)lPart;
				System.out.println("File  name is " + lPart.getName() + " Value is " + lFilePart.getFileName());
				System.out.println("File  content type is :" + lFilePart.getContentType());
				System.out.println("Server Name = " + req.getServerName());
				lVm.setFileName(lFilePart.getFileName());
				if(new StringBuffer(lFilePart.getFileName()).indexOf(".mp3") > 0 )
				{
					lVm.setContentType("audio/mpeg");
					System.out.println("Setting to audio/mpeg");
				}
				else
				{
				lVm.setContentType(lFilePart.getContentType());
				}
				InputStream lImgStream = lFilePart.getInputStream();
			    Blob lVmBlob = new Blob(IOUtils.toByteArray(lImgStream));
	    		lVm.setVm(lVmBlob);
	    		lVm.setSubject(req.getParameter("subject"));
	    		
	    		
	    	}
		
	    }
	    
	    if(lVm.getVm() != null)
	    {
	    	/*Save the results to Db*/
	    	PersistenceManager lPm = PMF.get().getPersistenceManager();
	    	
	    	
	    	try{
	    		
	    		lPm.makePersistent(lVm);
	     		
	    		 
	    	}
	    	catch(Exception e)
	    	{
	    		e.printStackTrace();
	    	}
	    	finally
	    	{
	    		lPm.close();
	    	}
	    }
		
	    ServiceProvider lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("voicecall");
		lSp.send(lVm);
		
		lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("email");
		lSp.send(lVm);
	    
		lSp = AppStackFactory.getStack().getServiceProviderFactory().getServiceProvider("sms");
		lSp.send(lVm);
		
		resp.setContentType("text/plain");
		resp.getWriter().println("Thank you, message sent");
		resp.getWriter().close();
	}
}
